Skip to content

Conversation

@dimbleby
Copy link
Contributor

@dimbleby dimbleby commented Oct 26, 2025

Resolves: python-poetry#10599

Replace the naive textual manipulation in create_nested_marker with direct manipulation of the objects, relying on their __str()__ implementation to be good.

To make sure that the text field is recalculated here, I made it an init=False field. That has the knock-on effect that we always normalize versions - which seems fine to me, probably a good thing.

There is one testcase in poetry proper that will need a one-character tweak to expect the newly normalized version number.

  • Added tests for changed code.
  • Updated documentation for changed code.

@sourcery-ai
Copy link

sourcery-ai bot commented Oct 26, 2025

Reviewer's Guide

Replace manual text padding in nested marker creation with direct dataclass manipulation and rely on version str; make PEP440Version.text init=False to enforce normalization via to_string and remove parser text assignment; update tests to expect the new normalized version texts and adjusted markers.

Class diagram for updated PEP440Version and Release handling

classDiagram
    class PEP440Version {
        +Release release
        +ReleaseTag pre
        +ReleaseTag post
        +ReleaseTag dev
        +LocalSegmentType local
        -str text (init=False)
        -tuple _compare_key (init=False)
        +__post_init__()
        +to_string()
        +_make_compare_key()
    }
    class Release {
        +int major
        +int minor
        +int patch
    }
    PEP440Version --> Release : release
    PEP440Version --> ReleaseTag : pre
    PEP440Version --> ReleaseTag : post
    PEP440Version --> ReleaseTag : dev
    PEP440Version --> LocalSegmentType : local
Loading

Class diagram for create_nested_marker changes

classDiagram
    class create_nested_marker {
        +constraint
        +version
        +min_name
        +op
        +part
    }
    class Release {
        +major
        +minor
        +patch
    }
    class Version {
        +release
        +stable
        +__str__()
    }
    create_nested_marker --> Version : version
    Version --> Release : release
Loading

File-Level Changes

Change Details Files
Replace naive textual padding in nested marker creation with direct object manipulation and rely on str
  • use dataclasses.replace to pad missing release fields
  • remove manual '.0' padding logic
  • construct marker expression using version str
src/poetry/core/packages/utils/utils.py
Make version.text field init=False and always normalized
  • change text dataclass field to init=False in PEP440Version
  • always set text via to_string() in post_init
  • remove explicit text assignment in parser
src/poetry/core/version/pep440/version.py
src/poetry/core/version/pep440/parser.py
Update tests to expect normalized version text and adjusted markers
  • add normalized parameter in test_parse_valid
  • tweak expected text in test_version and test_parse_constraint
  • update constraint tests to use new 'a1' and '.post' formats
  • add new marker test for rc1 in utils tests
tests/constraints/version/test_version.py
tests/constraints/version/test_parse_constraint.py
tests/packages/utils/test_utils.py

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey there - I've reviewed your changes and they look great!

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location> `tests/constraints/version/test_parse_constraint.py:456` </location>
<code_context>
+        ("^0-alpha.1", ">=0a1,<1"),
+        ("^0.1-alpha.1", ">=0.1a1,<0.2"),
+        ("^0.0.2-alpha.1", ">=0.0.2a1,<0.0.3"),
+        ("^0.1.2-alpha.1", ">=0.1.2a1,<0.2.0"),
         ("~1", ">=1,<2"),
         ("~1.0", ">=1.0,<1.1"),
</code_context>

<issue_to_address>
**suggestion (testing):** Missing test for normalization of post-release and dev-release constraints.

Please add tests for normalization of post-release (e.g., "1.0.0-post1") and dev-release (e.g., "1.0.0.dev1") constraints to cover these cases.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

@radoering radoering changed the title fix poetry #10599 fix normalization of python_full_version markers with pre-release versions Oct 31, 2025
@radoering radoering enabled auto-merge (squash) October 31, 2025 05:19
@radoering radoering merged commit acc1bc9 into python-poetry:main Oct 31, 2025
22 checks passed
@dimbleby dimbleby deleted the fix-10599 branch October 31, 2025 08:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

InvalidMarkerError doesn't identify the module it occurred for

2 participants